{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9c48213d-6e6a-4c10-838a-2a7c710c3a05",
   "metadata": {},
   "source": [
    "# Simple Index Demo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50d3b817-b70e-4667-be4f-d3a0fe4bd119",
   "metadata": {},
   "source": [
    "#### Load documents, build the GPTSimpleVectorIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "690a6918-7c75-4f95-9ccc-d2c4a1fe00d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))\n",
    "\n",
    "from gpt_index import GPTSimpleVectorIndex, SimpleDirectoryReader\n",
    "from IPython.display import Markdown, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "03d1691e-544b-454f-825b-5ee12f7faa8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load documents\n",
    "documents = SimpleDirectoryReader('../paul_graham_essay/data').load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ad144ee7-96da-4dd6-be00-fd6cf0c78e58",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:gpt_index.token_counter.token_counter:> [build_index_from_nodes] Total LLM token usage: 0 tokens\n",
      "> [build_index_from_nodes] Total LLM token usage: 0 tokens\n",
      "> [build_index_from_nodes] Total LLM token usage: 0 tokens\n",
      "> [build_index_from_nodes] Total LLM token usage: 0 tokens\n",
      "INFO:gpt_index.token_counter.token_counter:> [build_index_from_nodes] Total embedding token usage: 17430 tokens\n",
      "> [build_index_from_nodes] Total embedding token usage: 17430 tokens\n",
      "> [build_index_from_nodes] Total embedding token usage: 17430 tokens\n",
      "> [build_index_from_nodes] Total embedding token usage: 17430 tokens\n"
     ]
    }
   ],
   "source": [
    "index = GPTSimpleVectorIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2bbccf1d-ac39-427c-b3a3-f8e9d1d12348",
   "metadata": {},
   "outputs": [],
   "source": [
    "# save index to disk\n",
    "index.save_to_disk('index_simple.json')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "197ca78e-1310-474d-91e3-877c3636b901",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load index from disk\n",
    "index = GPTSimpleVectorIndex.load_from_disk('index_simple.json')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6caf93b-6345-4c65-a346-a95b0f1746c4",
   "metadata": {},
   "source": [
    "#### Query Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85466fdf-93f3-4cb1-a5f9-0056a8245a6f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# set Logging to DEBUG for more detailed outputs\n",
    "response = index.query(\"What did the author do growing up?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bdda1b2c-ae46-47cf-91d7-3153e8d0473b",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0da9092e",
   "metadata": {},
   "source": [
    "**Query Index with custom embedding string**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d57f2c87",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gpt_index.indices.query.schema import QueryBundle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbecbdb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "query_bundle = QueryBundle(\n",
    "    query_str=\"What did the author do growing up?\", \n",
    "    custom_embedding_strs=['The author grew up painting.']\n",
    ")\n",
    "response = index.query(query_bundle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4d1e028",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5636a15c-8938-4809-958b-03b8c445ecbd",
   "metadata": {},
   "source": [
    "#### Get Sources"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db22a939-497b-4b1f-9aed-f22d9ca58c92",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(response.get_formatted_sources())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0c5d984-db20-4679-adb1-1ea956a64150",
   "metadata": {},
   "source": [
    "#### Query Index with LlamaLogger\n",
    "\n",
    "Log intermediate outputs and view/use them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59b8379d-f08f-4334-8525-6ddf4d13e33f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gpt_index.logger import LlamaLogger\n",
    "llama_logger = LlamaLogger()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa281be0-1c7d-4d9c-a208-0ee5b7ab9953",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = index.query(\n",
    "    \"What did the author do growing up?\",\n",
    "    llama_logger=llama_logger,\n",
    "    similarity_top_k=2,\n",
    "    # response_mode=\"tree_summarize\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7d65c9ce-45e2-4655-adb1-0883470f2490",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# get logs\n",
    "llama_logger.get_logs()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_index",
   "language": "python",
   "name": "llama_index"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
